package com.store.web.security;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.image.ImageObserver;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.ProductDAO;
import com.ross.web.dao.UsersDAO;
import com.ross.web.domain.Users;
import com.ross.web.util.Encrypter;

public class LoginAction extends ActionSupport {
	
	private Users userAccess;
	private String username;
	private String password;
	private List userInfo;
	private int logInAttempts = 0;
	
	private HttpServletRequest request;
	
	UsersDAO usersDAO = new UsersDAO();
	Encrypter e = new Encrypter();
	
	private ProductDAO productDAO = new ProductDAO();
	public String doLoginAction() throws Exception{
		
		request = ServletActionContext.getRequest();
		userAccess = usersDAO.getUserAccessByUsername(username, e.encrypt(password));
		
		if(userAccess== null){
			addActionError(getText("login.failed"));
			return ERROR;
			
		}else if(userAccess.getStatus().equalsIgnoreCase("disabled")){
			addActionError(getText("login.locked"));
			return ERROR;

		
		}else{
			
			userInfo = usersDAO.validateUsernameByPassword(username, e.encrypt(password));
			
			if(userInfo.isEmpty()){
				logInAttempts = userAccess.getLoginAttempt()+1;
				if(userAccess.getLoginAttempt()>1){
					userAccess.setLoginAttempt(logInAttempts);
					userAccess.setStatus("disabled");
					
				}else{
					userAccess.setLoginAttempt(logInAttempts);
				}

						usersDAO.update(userAccess);
				
				
				addActionError(getText("login.failed"));
				return ERROR;
			}else{
				SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
				Date date = new Date();
				String currDate = sdf.format(date);

				userAccess.setLoginAttempt(logInAttempts);
				usersDAO.update(userAccess);
			
				int i = productDAO.getAllProductOutofStock().size();
				
				if(i>0){
					request.getSession().setAttribute("warning", "show");
				}else{
					request.getSession().setAttribute("warning", "hide");
				}
				request.getSession().setAttribute("currDate", currDate);
				request.getSession().setAttribute("userAccess", userAccess);
				
			}
			
		
		}
		
		return SUCCESS;
	}

	public Users getUserAccess() {
		return userAccess;
	}

	public void setUserAccess(Users userAccess) {
		this.userAccess = userAccess;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	

}
